package start.doubudouyin.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import start.doubudouyin.entity.Admininfo;
import start.doubudouyin.entity.Adminregister;
import start.doubudouyin.service.AdmininfoService;
import start.doubudouyin.service.AdminregisterService;
import start.doubudouyin.utils.*;

import java.util.List;

@RestController
@RequestMapping("/admininfo")
@CrossOrigin(origins = "http://localhost:5173/")
public class AdminController {
    @Resource
    private AdmininfoService admininfoService;

    //管理员登录
    @GetMapping("/adminlogin")
    public Result AdminLogin(@RequestParam("phone") String phone, @RequestParam("password") String password){
        phone=admininfoService.isPhoneExist(phone);
        if(phone==null){
            return Result.error("300","该手机号未注册，请先注册!");
        }else{
            Integer adminId = admininfoService.AdminLogin(phone, MD5.Encrypt(password));
            if (adminId == null) {
                return Result.error("300", "账号或密码错误，请重新输入!");
            }
            if(admininfoService.isAdminOnline(adminId)==1){
                return Result.error("300","管理员在线，请勿重复登陆!");
            }
            if(admininfoService.updateonlineStatus(1,adminId)<=0){
                return Result.error("300","系统错误，请稍后再试!");
            }
            return Result.success("登录成功!",adminId);
        }
    }

    //根据id获取管理员信息
    @GetMapping("/getadmininfo")
    public Result getAdmininfo(@RequestParam("adminId") Integer adminId){
        new Admininfo();
        Admininfo admininfo;
        admininfo = admininfoService.getById(adminId);
        admininfo.setPassword(null);
        admininfo.setIdcard(null);
        return Result.success(admininfo);
    }

    //管理员修改密码
    @PutMapping("/updatepassword")
    public Result UpdatePassword(@RequestParam("phone") String phone,@RequestParam("idcard") String idcard,
                                 @RequestParam("password") String password) {
        if(admininfoService.VerifyAdmin(phone,MD5.Encrypt(idcard)) == null) {
            return Result.error("300","管理员不存在，请检查手机号和身份证号");
        }
        if(admininfoService.UpdatePassword(phone,MD5.Encrypt(idcard),MD5.Encrypt(password))<=0){
            return Result.error("300", "系统错误，请稍后再试!");
        }
        return Result.success();
    }
    //查询所有管理员信息
    @GetMapping("/findAll")
    public Result findAll(){
        List<Admininfo> list=admininfoService.findAll();
        if(list==null){
            return Result.error("300","获取管理员信息失败");
        }else{
            return Result.success(list);
        }
    }

    //根据id删除管理员
    @DeleteMapping("/deletebyId")
    public Result deleteById(@RequestParam("adminId") Integer adminId){
        if(admininfoService.deletebyId(adminId)>0){
            return Result.success("删除成功");
        }else{
            return Result.error("300","删除失败");
        }
    }

    //退出登录
    @PutMapping("/adminLogout")
    public Result AdminLogout(@RequestParam("adminId") Integer adminId) {
        if (admininfoService.updateonlineStatus(0, adminId) <= 0) {
            return Result.error("300", "系统错误，请稍后再试!");
        }
        return Result.success("退出成功!");
    }

    //根据id和身份证号修改密码
    @PutMapping("/updatePasswordByIdCard")
    public Result updatePasswordByIdCard(@RequestParam("adminId") String adminId, @RequestParam("idcard") String idcard,
                                         @RequestParam("newPassword") String newPassword){
        if (admininfoService.VerifyId(adminId, MD5.Encrypt(idcard)) == null) {
            return Result.error("300", "身份证号错误");
        }
        if (admininfoService.updatePasswordById(MD5.Encrypt(newPassword),adminId) <= 0) {
            return Result.error("300", "系统错误，请稍后再试!");
        }
        return Result.success();
    }

    //    根据id、旧密码修改密码
    @PutMapping("/updatePasswordByOld")
    public Result updatePasswordByOld(@RequestParam("adminId") String adminId,@RequestParam("password") String password,
                                      @RequestParam("newPassword") String newPassword){
        if (admininfoService.VerifyPassword( adminId,MD5.Encrypt(password)) == null) {
            return Result.error("300", "密码错误");
        }
        if(admininfoService.updatePasswordById(MD5.Encrypt(newPassword),adminId)<=0){
            return Result.error("300","系统错误，请稍后再试!");
        }else{
            return Result.success();
        }
    }

}
